<template>
	<view>
		<mescroll-body ref="mescrollRef" @init="mescrollInit" @down="downCallback" @up="upCallback" :up="upOption">
			<good-list :list="goods" @delProduct="deleteProduct"></good-list>
		</mescroll-body>
	</view>
</template>

<script>
import GoodList from '@/pages/user/components/good-list.vue';
import MescrollMixin from '@/components/mescroll-uni/mescroll-mixins.js';
var _self;
export default {
	mixins: [MescrollMixin], // 使用mixin (在main.js注册全局组件)
	components: {
		GoodList
	},
	data() {
		return {
			goods: [],
			totalPage: 0, //总页数
			upOption: {
				noMoreSize: 3, //如果列表已无数据,可设置列表的总数量要大于半页才显示无更多数据;避免列表数据过少(比如只有一条数据),显示无更多数据会不好看
				empty: {
					tip: '~ 暂无收藏内容 ~' // 提示
				}
			}
		};
	},
	onLoad(options) {
		_self = this;
	},
	methods: {
		/*下拉刷新的回调 */
		downCallback() {
			_self.mescroll.resetUpScroll();
		},

		/*上拉加载的回调: 其中page.num:当前页 从1开始, page.size:每页数据条数,默认10 */
		upCallback(page) {
			//联网加载数据
			_self.mescroll.showUpScroll();
			_self
				.getListDataFromNet(page.num, page.size)
				.then(curPageData => {
					if (page.num == 1) _self.goods = []; //如果是第一页需手动制空列表
					_self.goods = _self.goods.concat(curPageData); //追加新数据
					_self.mescroll.endByPage(curPageData.length, _self.totalPage);
					_self.mescroll.hideUpScroll();
				})
				.catch(() => {
					//联网失败, 结束加载
					_self.mescroll.endErr();
				});
			_self.mescroll.hideUpScroll();
		},

		/* 获取查询数据 */
		getListDataFromNet(pageNum, pageSize, successCallback, errorCallback) {
			try {
				const param = {
					currentPage: pageNum,
					pageSize: pageSize
				};
				let promise = new Promise(function(resolve, reject) {
					_self.$http.httpRequest('userFavorite/list', param, true).then(res => {
						if (res._tranStatus) {
							_self.totalPage = res._data.totalPage;
							resolve(res._data.pageItems);
						}
					});
				});
				return promise;
			} catch (e) {
				return Promise.reject(e);
			}
		},

		deleteProduct(id, index) {
			const param = {
				id: id
			};
			_self.$http.httpRequest('userFavorite/delete', param, true).then(res => {
				if (res._tranStatus) {
					_self.goods.splice(index, 1);
				}
			});
		}
	}
};
</script>

<style></style>
